package com.doudou.collection.adapter.controller;


import com.doudou.collection.domain.service.CommentService;
import com.doudou.collection.infrastructure.common.security.CurrentPrincipal;
import com.doudou.collection.infrastructure.common.security.SecurityUtils;
import com.doudou.collection.infrastructure.common.web.ResultVO;
import com.doudou.collection.infrastructure.core.security.CustomUserDetails;
import com.doudou.collection.orm.dto.CommentsDTO;
import com.doudou.collection.orm.vo.CommentBlogVO;
import com.doudou.collection.orm.vo.CommentPersonalVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @ClassName CommentsController<br>
 * @Description 评论模块<br>
 * @Author 高绮雯<br>
 * @Date 2023/6/23 20:02<br>
 * @Version 1.0<br>
 */
@Api(tags = "06.评论管理模块")
@Slf4j
@RestController
@RequestMapping("/v1/comment")
public class CommentsController {

    @Autowired
    private CommentService commentService;


    @PostMapping("{blogId}/insert")
    @ApiOperation(value = "文章中发布评论功能")
    @ApiOperationSupport(order = 100)
    public ResultVO insertComment(CommentsDTO commentsDTO, @PathVariable Long blogId){
        log.debug("正在处理发布评论请求,commentsDTO为{}",commentsDTO);

        CurrentPrincipal loginUser = SecurityUtils.getLoginUser();
        commentService.insertComment(commentsDTO,loginUser.getUserId(),blogId);
        return ResultVO.ok();

    }

    @DeleteMapping ("/{id}/delete")
    @ApiOperation(value = "根据评论id删除评论功能")
    @ApiOperationSupport(order = 200)
    public ResultVO delete(@PathVariable Long id){
        log.debug("正在处理根据评论id删除评论请求,评论id为{}",id);
        commentService.delete(id);
        return ResultVO.ok();
    }

    @GetMapping ("/{id}/selectAll")
    @ApiOperation(value = "文章中评论展示")
    @ApiOperationSupport(order = 400)
    public ResultVO list(@PathVariable Long id){
        log.debug("正在处理根据文章id查询评论列表功能,文章id为 {}", id);
        List<CommentBlogVO> list =commentService.list(id);
        return ResultVO.ok(list);
    }

    @GetMapping("/{id}/selectPersonalAll")
    @ApiOperation(value = "个人所有评论展示")
    @ApiOperationSupport(order = 401)
    public ResultVO listing(@PathVariable Long id){
        log.debug("正在处理根据用户id查询评论列表功能,用户id为 {}", id);
        List<CommentPersonalVO> listing =commentService.listing(id);
        return ResultVO.ok(listing);
    }

}
